///////////////////////////////////////////////////////////////////////////////////////
//
//	Copyright (c) 2011 staticfunction
//	
//	---------Author Information---------
//
//		Name: James Roland Cabresos
//		Email: j.cabresos@yahoo.com
//		Blog: staticfunction.wordress.com
//
//	---------Author Information---------
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//	
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//	
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////////////

package kola.controls
{
	import flash.events.Event;
	
	import mx.core.mx_internal;
	import mx.events.FlexEvent;
	
	import spark.components.supportClasses.ButtonBase;
	
	/**
	 *  Dispatched when the <code>selected</code> property 
	 *  changes for the ToggleButtonBase control. 
	 * 
	 *  This event is dispatched only when the 
	 *  user interacts with the control by using the mouse.
	 *
	 *  @eventType flash.events.Event.CHANGE
	 *  
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion Flex 4
	 */
	[Event(name="change", type="flash.events.Event")]
	
	/**
	 *  Up State of the Button when it's checked
	 *  
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion Flex 4
	 */
	[SkinState("upAndChecked")]
	
	/**
	 *  Over State of the Button when it's checked
	 *  
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion Flex 4
	 */
	[SkinState("overAndChecked")]
	
	/**
	 *  Down State of the Button when it's checked
	 *  
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion Flex 4
	 */
	[SkinState("downAndChecked")]
	
	/**
	 *  Disabled State of the Button when it's checked
	 *  
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion Flex 4
	 */
	[SkinState("disabledAndChecked")]
	
	/**
	 *  Up State of the Button when it's indeterminate
	 *  
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion Flex 4
	 */
	[SkinState("upAndIndeterminate")]
	
	/**
	 *  Over State of the Button when it's indeterminate
	 *  
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion Flex 4
	 */
	[SkinState("overAndIndeterminate")]
	
	/**
	 *  Down State of the Button when it's indeterminate
	 *  
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion Flex 4
	 */
	[SkinState("downAndIndeterminate")]
	
	/**
	 *  Disabled State of the Button when it's indeterminate
	 *  
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion Flex 4
	 */
	[SkinState("disabledAndIndeterminate")]

	//--------------------------------------
	//  Other metadata
	//--------------------------------------
	
	[AccessibilityClass(implementation="spark.accessibility.CheckBoxAccImpl")]
	
	//--------------------------------------
	//  Other metadata
	//--------------------------------------
	use namespace mx_internal;
	/**
	 * The Kola CheckBox control differs from the default Flex spark CheckBox
	 * control. This custom checkbox have three states namely "unchecked", "indeterminate" and
	 * "checked". Typically you use a tri-state checkbox for a tree list parent item. An 
	 * indeterminate value represents that a parent has items that are checked but not all of it's 
	 * children are checked (represented by a black square) and a checked value if all of it's 
	 * children are checked (represented by a check mark). 
	 * 
	 * @author James Roland Cabresos
	 * 
	 */
	public class CheckBox extends ButtonBase
	{
		/**
		 * Checkbox value if the checkbox is unchecked.
		 */
		public static const UNCHECKED:String = "unchecked";
		
		/**
		 * Checkbox value if the checkbox is indeterminate or partially checked.
		 */
		public static const INDETERMINATE:String = "indeterminate";
		
		/**
		 * Checkbox value if the checkbox is fully checked.
		 */
		public static const CHECKED:String = "checked";
		
		
		/**
		 *  @private
		 *  Placeholder for mixin by CheckBoxAccImpl.
		 */
		mx_internal static var createAccessibilityImplementation:Function;
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		/**
		 *  Constructor. 
		 *  
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion Flex 4
		 */
		public function CheckBox()
		{
			super();
		}
		
		private var _value:String = UNCHECKED;
		
		[Bindable("change")]
		public function get value():String
		{
			return _value;
		}

		public function set value(value:String):void
		{
			if(value == UNCHECKED || value == INDETERMINATE ||
				value == CHECKED)
			{
				_value = value;		
			}
			else
				return;
			
			dispatchEvent(new FlexEvent(FlexEvent.VALUE_COMMIT));
			
			invalidateSkinState();
		}
		
		
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Properties
		//
		//--------------------------------------------------------------------------
		
		//----------------------------------
		//  suggestedFocusSkinExclusions
		//----------------------------------
		/** 
		 * @private 
		 */     
		private static const focusExclusions:Array = ["labelDisplay"];
		
		/**
		 *  @private
		 */
		override public function get suggestedFocusSkinExclusions():Array
		{
			return focusExclusions;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Overridden methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 *  @private
		 */
		override protected function initializeAccessibility():void
		{
			if (CheckBox.createAccessibilityImplementation != null)
				CheckBox.createAccessibilityImplementation(this);
		}
		
		override protected function buttonReleased():void
		{
			if(value == UNCHECKED || value == INDETERMINATE)
				value = CHECKED;
			else
				value = UNCHECKED;
			
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		override protected function getCurrentSkinState():String
		{
			var skinState:String = super.getCurrentSkinState();
			
			switch(value)
			{
				case CHECKED:
					skinState+="AndChecked";
					break;
				
				case INDETERMINATE:
					skinState+="AndIndeterminate";
					break;	
			}
			
			return skinState;
		}
	}
}